let user_course_num=[];
let course_per=[];

let userId_const = localStorage.getItem("userId");
let userName_const = localStorage.getItem("userName");

let username = $(".dropdown-user-name");
username.find('i').remove();
let user_name = '<i>你好，'+userName_const+'老师</i>';
username.append(user_name);                       //生成导航栏的老师名字
username = $("#user_name");
username.find('span').remove();
user_name = '<span>'+userName_const+'老师</span>'      //生成导航栏下拉框的老师名字
username.append(user_name);

let run=true;
let contant_all=[];
function checkURL(){
    $("#course-chosen tr").find('a').click(function(){
        let id = $(this).parents("tr").find('th').text();
        window.open('./content-input.html?courseId='+id,'_self');
    })
}
// var run=true;
function init(){
    $.ajax({
        url:'/lv_course/showAllCourse',
        type:'get',
        dataType:'json',
        success:function (data){
            $("#lv-course").find('tr').remove();
            console.log(data);
            for(let i in data){
                let tr;


                tr = $("<tr>");
                let th1=$('<th scope="row">'+data[i].userId+'</th>' );

                let th2=$('<td>' +data[i].courseId+'</td>');

                let th3=$('<td>' +data[i].courseName+'</td>');
                let th4=$('<td>' +data[i].graduationPoint+'</td>');
                tr.append(th1);
                tr.append(th2);
                tr.append(th3);
                tr.append(th4);
                btn_delete=$("<td> <input type=button value='删除' class='lv_button' /> </td>");
                btn_modify=$("<td> <input type=button value='修改' class='lv_button' /> </td>");
                tr.append(btn_delete);
                tr.append(btn_modify);

                $("#lv-course").append(tr);

                //删除课程

                btn_delete.click(function (){


                    let tr_table=$(this).parent();
                    let td=tr_table.find("td").eq(0).html();
                    alert(td);
                    $.ajax({
                        url:'/lv_course/DeleteCourse',
                        type:'post',
                        dataType: 'json',
                        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        data:{"courseId":td},

                        success:function (data){
                            console.log(data);
                            if(data)
                                alert("课程目标删除成功");
                            else
                            {

                                alert("删除课程目标失败");
                            }
                        }




                    })

                    $(this).parent().remove();





                })
                //

                //修改课程
                btn_modify.click(function (){
                    //   调用模态框
                    //     alert("修改课程目标");
                    //显示修改框
                    // $(".lv_hide").removeClass("lv_hide");
                    //根据课程目标ID 返回一条数据
                    let t1=$(this).parent().find("td").eq(0).html();
                    $.ajax({
                        url:'/lv_course/SelectCourseById',
                        type:'get',
                        dataType: 'json',
                        // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        data:{"courseId":t1},

                        success:function (data){
                            console.log(data);
                            if(1)
                            {

                                alert("课程目标是"+t1);
                                $(".lv_course_hide").removeClass("lv_course_hide");

                                $("#modify_userid").val(data.userId);
                                $("#modify_course_id").val(data.courseId);
                                $("#modify_course_name").val(data.courseName);
                                $("#modify_grad_point").val(data.graduationPoint);
                            }
                            else
                            {
                                alert("修改课程目标失败");
                            }
                        }




                    })
                    
                })






            }

        }
    });
}






$(document).ready(function(){

    //    完成初始化
    init();


     // 增加课程目标
    $("#lv_course_addcourse").click(function (){
        let UserId =$("#lv_course_userid").val();
        let CourseId = $("#lv_course_courseid").val();
        let CourseName=$("#lv_course_coursename").val();
        let CourseGradPoint=$("#lv_course_grad_point").val();

        console.log(UserId,CourseId,CourseName,CourseGradPoint);
        $.ajax({
            url:'/lv_course/addCourse',
            type:'post',
            dataType: 'json',
            // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
            //private Integer courseId;
            //
            //     private String courseName;
            //
            //     private Integer userId;
            //
            //     private String graduationPoint;
            data:{"userId":UserId,"courseId":CourseId,
                "courseName":CourseName,"graduationPoint":CourseGradPoint},

            success:function (data){
                console.log(data);
                if(data)
                {
                    alert("课程目标增加成功");
                    $("#lv_course_userid").val("");
                    $("#lv_course_courseid").val("");
                    $("#lv_course_coursename").val("");
                    $("#lv_course_grad_point").val("");

                    init();
                }
                else
                {

                    alert("增加课程目标失败");
                }
            }




        })
    })


   // 取消修改


    $("#cancel_modifyCourse").click(function (){

        $(".lv_course_modal").addClass("lv_course_hide");
        $(".lv_course_shade").addClass("lv_course_hide");

    })

   // 提交修改


    $("#finish_modifyCourse").click(function () {
        let UserId =$("#modify_userid").val();
        let CourseId = $("#modify_course_id").val();
        let CourseName=$("#modify_course_name").val();
        let GradPoint=$("#modify_grad_point").val();

        console.log(UserId,CourseId,CourseName,GradPoint);
        $.ajax({
            url:'/lv_course/updateCourseById',
            type:'post',
            dataType: 'json',
            // contentType:'application/x-www-form-urlencoded; charset=UTF-8',
            data:{"userId":UserId,"courseId":CourseId,
                "courseName":CourseName,"graduationPoint":GradPoint},

            success:function (data){
                console.log(data);
                if(1)
                {

                    alert("课程修改成功");

                    $(".lv_course_modal").addClass("lv_course_hide");
                    $(".lv_course_shade").addClass("lv_course_hide");
                    //刷新
                    init();
                }
                else
                {

                    alert("增加课程目标失败");
                }
            }




        })


    })








})


